package com.yupi.apiback.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.apiback.common.BaseResponse;
import com.yupi.apiback.common.ResultUtils;
import com.yupi.apiback.model.dto.interfaceInfo.InterfaceAddRequest;
import com.yupi.apiback.model.dto.interfaceInfo.InterfaceInvokingRequest;
import com.yupi.apiback.model.dto.interfaceInfo.InterfacePageRequest;
import com.yupi.apiback.model.dto.interfaceInfo.InterfaceUpdateRequest;
import com.yupi.apiback.model.entity.InterfaceInfo;
import com.yupi.apiback.service.InterfaceInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author yanghaifeng
 * @date 2024/1/18 15:52
 */
@Api(tags = "接口信息")
@RestController
@RequestMapping("/interface")
public class InterfaceInfoController {

    @Autowired
    private InterfaceInfoService service;

    @ApiOperation("新增")
    @PostMapping("/add")
    public BaseResponse<Boolean> add(@RequestBody InterfaceAddRequest dto) {
        return ResultUtils.success(service.add(dto));
    }

    @ApiOperation("删除接口")
    @GetMapping("/remove")
    public BaseResponse<Boolean> remove(@ApiParam("接口Id") @RequestParam("id") Long id) {
        return ResultUtils.success(service.remove(id));
    }

    @ApiOperation("批量删除")
    @PostMapping("/removeList")
    public BaseResponse<Boolean> removeList(@ApiParam("接口Id") @RequestBody List<Long> ids) {
        return ResultUtils.success(service.removeList(ids));
    }

    @ApiOperation("修改")
    @PostMapping("/edit")
    public BaseResponse<Long> edit(@RequestBody InterfaceUpdateRequest dto) {
        return ResultUtils.success(service.edit(dto));
    }

    @ApiOperation("查询接口信息详情")
    @GetMapping("/queryDetails")
    public BaseResponse<InterfaceInfo> queryDetails(@ApiParam("接口Id") @RequestParam("id") Long id) {
        return ResultUtils.success(service.queryDetails(id));
    }

    @ApiOperation("分页查询")
    @PostMapping("/page")
    public BaseResponse<Page<InterfaceInfo>> page(@RequestBody InterfacePageRequest condition, Page<InterfaceInfo> page, HttpServletRequest request) {
        return ResultUtils.success(service.page(condition, page, request));
    }

    @ApiOperation("接口上线")
    @GetMapping("/online")
    public BaseResponse<Boolean> online(@ApiParam("接口Id") @RequestParam("id") Long id) {
        return ResultUtils.success(service.online(id));
    }

    @ApiOperation("接口下线")
    @GetMapping("/offline")
    public BaseResponse<Boolean> offline(@ApiParam("接口Id") @RequestParam("id") Long id) {
        return ResultUtils.success(service.offline(id));
    }

    @ApiOperation("接口调用")
    @PostMapping("/invoking")
    public BaseResponse<Object> invoking(@RequestBody InterfaceInvokingRequest dto, HttpServletRequest request) {
        return ResultUtils.success(service.invoking(dto, request));
    }
}
